package sk.tuke.serviceDao;

import java.sql.Timestamp;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;

import sk.tuke.persistance.DiagnosePo;
import sk.tuke.persistance.HealthCardPo;
import sk.tuke.persistance.MedicalNotePo;
import sk.tuke.persistance.MedicalRecordPo;
import sk.tuke.persistance.PatientPo;

public class HealthcardManagementDao extends AbstractDao {

	public HealthCardPo createHealthCard(String name, String description, java.sql.Timestamp date, java.util.Set<MedicalRecordPo> medicalRecord,
			PatientPo patient) {
		Session session = getSession();
		session.beginTransaction();

		HealthCardPo healthCard = new HealthCardPo();

		healthCard.setDateFrom(date);
		healthCard.setDescription(description);
		healthCard.setMedicalRecord(medicalRecord);
		healthCard.setPatient(patient);

		session.save(healthCard);
		session.getTransaction().commit();
		session.close();
		return healthCard;
	}

	public HealthCardPo getHealthCard(Integer number) {
		return findEntityByNumber(HealthCardPo.class, number);
	}

	public HealthCardPo updateHealthCard(Integer existing, String name, String description, java.sql.Timestamp date,
			java.util.Set<MedicalRecordPo> medicalRecord, PatientPo patient) {
		Session session = getSession();

		HealthCardPo healthCard = (HealthCardPo) getHealthCard(existing);

		healthCard.setDateFrom(date);
		healthCard.setDescription(description);
		healthCard.setMedicalRecord(medicalRecord);
		healthCard.setPatient(patient);

		session.save(healthCard);
		session.getTransaction().commit();
		session.close();
		return healthCard;
	}

	public void deleteHealthCard(Integer existing) {
		Session session = getSession();

		HealthCardPo healthCard = (HealthCardPo) getHealthCard(existing);
		session.delete(healthCard);
	}

	public MedicalRecordPo getMedicalRecord(Integer number) {
		return findEntityByNumber(MedicalRecordPo.class, number);
	}

	public MedicalRecordPo updateMedicalRecord(Integer number, String name, Timestamp date, DiagnosePo diagnose) {
		Session session = getSession();
		session.beginTransaction();
		Query query = session.createQuery("select u from HealthCardPo u where number =  :number");
		query.setInteger("number", number);
		MedicalRecordPo medicalRecordPo = (MedicalRecordPo) query.uniqueResult();
		medicalRecordPo.setDate(date);
		medicalRecordPo.setName(name);
		medicalRecordPo.setDiagnose(diagnose);

		session.getTransaction().commit();
		session.close();

		return medicalRecordPo;
	}

	public void deleteMedicalRecord(Integer existing) {
		Session session = getSession();

		MedicalRecordPo medicalRecord = (MedicalRecordPo) getMedicalRecord(existing);
		session.delete(medicalRecord);
	}

	public MedicalNotePo createMedicalNote(String name, String description) {
		Session session = getSession();
		session.beginTransaction();
		MedicalNotePo medicalNotePo = new MedicalNotePo();
		medicalNotePo.setName(name);
		medicalNotePo.setDescription(description);
		session.save(medicalNotePo);
		session.getTransaction().commit();
		session.close();
		return medicalNotePo;
	}

	public MedicalRecordPo createMedicalRecord(Integer number, HealthCardPo healthCardPo, String name, Timestamp date, DiagnosePo diagnosePo, String status,
			Set<MedicalNotePo> medicalNotePos) {
		Session session = getSession();
		session.beginTransaction();
		MedicalRecordPo medicalRecordPo = new MedicalRecordPo();
		medicalRecordPo.setCard(healthCardPo);
		medicalRecordPo.setDate(date);
		medicalRecordPo.setDiagnose(diagnosePo);
		medicalRecordPo.setMedicalNote(medicalNotePos);
		medicalRecordPo.setName(name);
		medicalRecordPo.setNumber(number);
		medicalRecordPo.setStatus(status);
		session.save(medicalRecordPo);
		session.getTransaction().commit();
		session.close();
		return medicalRecordPo;
	}

}
